package com.example.administrator.fishbone.Fragment.HoneFragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.fishbone.bean.BackHandledFragment;
import com.example.administrator.fishbone.bean.DaoTask;
import com.example.administrator.fishbone.bean.Person;
import com.example.administrator.fishbone.R;
import com.example.administrator.fishbone.bean.ViewHolder;
import com.example.administrator.fishbone.adapter.CommonAdapter;
import com.yalantis.phoenix.PullToRefreshView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Created by Administrator on 2016/8/27.
 */
public class DynamicFragment extends Fragment {
    PullToRefreshView refreshView;
    ListView mlistView;
    CommonAdapter adapter;
    Person person;

    List<Person> list;
    SimpleDateFormat formata = new SimpleDateFormat("MM-dd");
    Date curDate = new Date(System.currentTimeMillis());//获取当前时间
    String str = formata.format(curDate);

    public static final int REFRESH_DELAY = 2000;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_dynamic, container, false);
        mlistView = (ListView) view.findViewById(R.id.dynamic_listView);
        refreshView = (PullToRefreshView) view.findViewById(R.id.pull_to_refresh);
        initVirw();

//        iiu();

        return view;
    }

//    private void iiu() {
//        person = new Person(getContext());
//        Cursor cursor = person.getcursorContent(0,10);
//        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
//               R.layout.home_dynamic_list_item,cursor,
//                new String[]{"last_name","title","content","time","name",
//                        "number","Data1","Data2"},new int[]{
//                R.id.dynamic_textIcon,
//                R.id.dynamic_title,
//                R.id.dynamic_content,
//                R.id.dynamic_time,
//                R.id.dynamic_name,
//                R.id.dynamic_number,
//                R.id.dynamic_data,
//                R.id.dynamic_data1,
//        });
//        mlistView.setAdapter(adapter);
//        mlistView.setOnItemClickListener(new ItemClickListener());
//    }
//private final class ItemClickListener implements AdapterView.OnItemClickListener{
//
//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        ListView listView = (ListView) parent;
//        Cursor cursor = (Cursor) listView.getItemAtPosition(position);
//        String personid = String.valueOf(cursor.getInt(cursor.getColumnIndex("id")));
//        Toast.makeText(getActivity(),personid,Toast.LENGTH_LONG).show();
//    }
//}


    private void initVirw() {
        refreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshView.setRefreshing(false);
                       data();
                    }
                }, REFRESH_DELAY);
            }
        });
    }

    private void data() {
        list = DaoTask.instance().queryAll();
        adapter = new CommonAdapter<Person>(getActivity(), list, R.layout.home_dynamic_list_item) {

            @Override
            public void setListener(int position, ViewHolder holder) {

            }

            @Override
            public void onClick(int position, View convertView, ViewHolder viewHolder) {

            }

            @Override
            public void setViewData(Person item, ViewHolder holder) {
                holder.setText(R.id.dynamic_textIcon, item.getTextIcon())
                        .setText(R.id.dynamic_title, item.getTitle())
                        .setText(R.id.dynamic_content, item.getContent())
                        .setText(R.id.dynamic_time, item.getTime())
                        .setText(R.id.dynamic_name, item.getName())
                        .setText(R.id.dynamic_number, item.getNumber())
                        .setText(R.id.dynamic_data, item.getData1())
                        .setText(R.id.dynamic_data1, item.getData2());

            }
        };
        mlistView.setAdapter(adapter);
    }


}

